package chen.videoedit;

import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import java.io.IOException;
import java.nio.ByteBuffer;

import static android.R.id.input;

public class EncodingVideoActivity extends BasisActivity {
    private String path = Environment.getExternalStorageDirectory() + "/test.mp4";
    private MediaExtractor extractor;
    private MediaCodec decoderCodec;
    private MediaCodec encodingCodec;

    @Override
    public int setlayoutResID() {
        return R.layout.activity_encoding_video;
    }

    @Override
    public void initialView(Bundle arg0) {
        findViewById(R.id.activity_encoding_video_bt).setOnClickListener(this);
    }

    @Override
    public void setActivity() {

    }

    @Override
    public void onClick(View v) {
        Log.e("lll", "点击了");
        try {
            prepare();
        } catch (IOException e) {
            Log.e("lll", e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 准备
     */
    private void prepare() throws IOException {
        extractor = new MediaExtractor();
        extractor.setDataSource(path);
        int trackCount = extractor.getTrackCount();
        for (int a = 0; a < trackCount; a++) {
            MediaFormat format = extractor.getTrackFormat(a);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith("video/")) {
                extractor.selectTrack(a);
                decoderCodec = MediaCodec.createDecoderByType(mime);
                decoderCodec.configure(format, null, null, 0);
                decoderCodec.start();
                break;
            }
        }


        MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 1280, 720);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 2500000);
        mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 24);
        mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
        mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1); //关键帧间隔时间 单位s

        encodingCodec = MediaCodec.createEncoderByType("video/avc");
        encodingCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        encodingCodec.start();




        ssss();
    }

    /**
     *
     * */
    private void ssss() {
        Log.e("lll","解码开始" );
        ByteBuffer[] inputS = decoderCodec.getInputBuffers();
        ByteBuffer[] outputS = decoderCodec.getOutputBuffers();
        MediaCodec.BufferInfo bufferInfo=new MediaCodec.BufferInfo();

        extractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
        while (true) {

            int inputBufIndex = decoderCodec.dequeueInputBuffer(-1);
            if (inputBufIndex < 0) break;
            ByteBuffer inputBuffer = inputS[inputBufIndex];
            inputBuffer.clear();
            int size = extractor.readSampleData(inputBuffer, 0);
            if (size < 0) break;
            extractor.advance();//读取下一步
            decoderCodec.queueInputBuffer(inputBufIndex, 0, size,  extractor.getSampleTime(),  0);//通知解码器数据放入
            int outputBufferIndex = decoderCodec.dequeueOutputBuffer(bufferInfo, 1000);//获取解码数据
            Log.e("lll","解码" +outputBufferIndex);
            if (outputBufferIndex >= 0) {
                ByteBuffer  out=outputS[outputBufferIndex];
                byte[] Bytes = new byte[bufferInfo.size];//BufferInfo内定义了此数据块的大小
//                out.get(Bytes);//将Buffer内的数据取出到字节数组中
//                Log.e("lll","解码");

                out.position(0);
                out.get(Bytes, 0, size);

                decoderCodec.releaseOutputBuffer(outputBufferIndex, false);
            }


        }


    }

}
